package com.bykj.taketurns.findfragment.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bykj.taketurns.R;
import com.bykj.taketurns.findfragment.activity.LevelThreeActivity;
import com.bykj.taketurns.findfragment.adapter.ThreeVideoAdapter;
import com.bykj.taketurns.findfragment.bean.ThreeVideoBean;
import com.bykj.taketurns.utils.Constant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

import static com.bykj.taketurns.R.id.pull_up_img;

/**
 * Created by Administrator on 2017/8/17.
 */

public class ThreeVideoFragment extends Fragment implements View.OnClickListener {

    private LevelThreeActivity activity;
    private String tid;
    private String title;
    private TextView title_three_video;
    private RelativeLayout three_alldetail_rela_video;
    private RelativeLayout three_detail_rela_video;
    private TextView three_tv_detail_video;
    private TextView three_tv_alldetail_video;
    private RecyclerView rv_data_three_video;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find_three_video,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initHeader();
        initWidget();
        setWidgetState();
    }

    private void initHeader() {
        activity = (LevelThreeActivity) getActivity();
    }

    private void initWidget() {

        title_three_video = getActivity().findViewById(R.id.title_three_video);
        three_alldetail_rela_video = getActivity().findViewById(R.id.three_alldetail_rela_video);
        three_detail_rela_video = getActivity().findViewById(R.id.three_detail_rela_video);
        three_tv_detail_video = getActivity().findViewById(R.id.three_tv_detail_video);
        three_tv_alldetail_video = getActivity().findViewById(R.id.three_tv_alldetail_video);
        rv_data_three_video = getActivity().findViewById(R.id.rv_data_three_video);

        getActivity().findViewById(R.id.iv_back_three_video).setOnClickListener(this);
        getActivity().findViewById(R.id.word_three_video).setOnClickListener(this);
        getActivity().findViewById(R.id.pull_down_img).setOnClickListener(this);
        getActivity().findViewById(pull_up_img).setOnClickListener(this);

    }

    private void setWidgetState() {
        rv_data_three_video.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_data_three_video.setHasFixedSize(true);

    }

    @Override
    public void onResume() {
        super.onResume();
        postData();
    }

    //进行网络请求数据
    public void postData() {

        if (getArguments() != null){
            Bundle bundle = getArguments();
            tid = bundle.getString("tid");
            title = bundle.getString("title");
        }

        title_three_video.setText(title);
        activity.map.put("tid", tid);

        OkGo.get(Constant.JHC_SERVICE +
                Constant.ServiceConstant.FIND_THREE_VIDEO + Constant.SetUrl(activity.map))     // 请求方式和请求url
                .tag(this)
                .execute(new StringCallback() {

                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        ThreeVideoBean threeVideoBean = activity.gson.fromJson(s, ThreeVideoBean.class);

                        three_tv_detail_video.setText("\t\t" + threeVideoBean.getTcontent());

                        three_tv_alldetail_video.setText("\t\t" + threeVideoBean.getTcontent());

                        rv_data_three_video.setAdapter(new ThreeVideoAdapter(getActivity(),threeVideoBean.getStoryvideo()));

                    }

                });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back_three_video:
                activity.finish();
                break;
            case R.id.word_three_video:
                FragmentManager manager = getActivity().getSupportFragmentManager();
                ThreeTextFragment threeTextFragment = (ThreeTextFragment) manager.findFragmentByTag("text");

                ((LevelThreeActivity)getActivity()).applyRotation(true, threeTextFragment, 0, 90,
                        ThreeVideoFragment.this);
                break;
            case R.id.pull_down_img:
                three_alldetail_rela_video.setVisibility(View.VISIBLE);
                three_detail_rela_video.setVisibility(View.GONE);
                break;
            case R.id.pull_up_img:
                three_alldetail_rela_video.setVisibility(View.GONE);
                three_detail_rela_video.setVisibility(View.VISIBLE);
                break;
        }
    }
}
